package com.jsonpfy.operations;

import java.util.Map.Entry;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.repackaged.org.json.JSONException;
import com.google.appengine.repackaged.org.json.JSONObject;

public class ConvertEntityToJson {

	public static JSONObject getJson(Entity result) {

		JSONObject jsonObject = new JSONObject();

		if (result != null) {

			try {

				if (result.getKey().getName() != null) {

					jsonObject.put("ID", result.getKey().getName());

				} else {

					jsonObject.put("ID", result.getKey().getId());
				}

				for (Entry<String, Object> entry : result.getProperties()
						.entrySet()) {

					jsonObject.put(entry.getKey(), entry.getValue());
				}

			} catch (JSONException e) {

				e.printStackTrace();
			}
		}

		return jsonObject;
	}

}